<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class company_controller extends common{
	function index_action(){

        //die('index_action');

		$this->rightinfo();
		$this->get_moblie();
        $CacheM=$this->MODEL('cache');
        $CacheList=$CacheM->GetCache(array('city','hy','com'));
		$this->yunset($CacheList);
		if((int)$_GET['three_cityid']){
			$this->yunset("cityname",$CacheList['city_name'][(int)$_GET['three_cityid']]);
		}
        foreach($_GET as $k=>$v){
			if($k!=""){
				$searchurl[]=$k."=".$v;
			}
		}
		$this->seo("firm");
		$searchurl=@implode("&",$searchurl);
		$this->yunset("searchurl",$searchurl);
		$this->yunset('backurl',Url('wap'));
		$this->yunset("topplaceholder","请输入企业关键字,如：有限公司...");
		$this->yunset("headertitle","公司搜索");
		$this->yuntpl(array('wap/company'));
	}

  //hjl 评论点赞
  
   function ajax_com_like_action(){

       $arr= array('is_liked'=>0 ,'status'=>0,'like_num'=>$like_num,'code'=>0);

       if($this->uid==0){
              
          $arr['code']=1;
          echo json_encode($arr);exit;


       }

        $comment_id = $_POST['comment_id'];
        $com_line_info  = $this->obj->DB_select_once(
        "comments_like",
        "`uid`={$this->uid} and comment_id='{$comment_id}'"
        );

        $like_num = $this->obj->DB_select_once('comments_like',"comment_id='$comment_id' and status=1",'count(*) as like_num');
        $like_num = $like_num['like_num'];

        //print_r($com_line_info);exit;

       

        if($com_line_info && !empty($com_line_info)){ //数据库中有了
           $arr['is_liked']=1;

           if($com_line_info['status']==1){
              $arr['status']= $com_line_info['status'];
              $this->obj->update_once(
                  'comments_like',
                  array('status'=>0),
                  array('uid'=>$this->uid,'comment_id'=>$comment_id)
                  );
                
           }else{
                $arr['status']= $com_line_info['status'];
                //echo $this->uid,'****',$comment_id;exit;
                //$res = $this->obj->update_once('comments_like',array('status'=>1),array('uid'=>$this->uid,'comment_id'=>$comment_id));
                $sql="update phpyun_comments_like set status=1 where uid='{$this->uid}' and comment_id='{$comment_id}'";
                $this->db->query($sql);
                //echo $res;exit;
           }





           //$arr['status'] = $com_line_info['status'];           
        }else{
             $arr['is_liked'] = 0;
             $value='';
             $value.="`uid`='".$this->uid."',";
             $value.="`comment_id`='".$comment_id."',";
             $value.="`status`='1'";
            $res = $this->obj->DB_insert_once("comments_like",$value);
            echo $res;exit;
        }

        echo json_encode($arr);exit;

   }

  

   //hjl: 公司的全部评价页面
   
   function show_pj_list_action(){
     
       $com_id = $_GET['id']; //企业id
     
       $UserinfoM=$this->MODEL('userinfo');
       $CacheM=$this->MODEL('cache');

       $CompanyM=$this->MODEL('company');

       $CacheList=$CacheM->GetCache(array('job','com','city','hy'));

       include PLUS_PATH."com.cache.php";
       //echo PLUS_PATH.'com.cache.php';exit;
       //print_R($comclass_name);exit;

       //好评的数量
       $goods_num = $this->obj->DB_select_once('comments',"com_id='$com_id' and zh_lv>3 and type=1",'count(*) as good_num');
       $good_num = $goods_num['good_num'];

       //差评的数量
       $bad_num = $this->obj->DB_select_once('comments',"com_id='$com_id' and zh_lv<=3 and type=1",'count(*) as bad_num');
      $bad_num = $bad_num['bad_num'];

      //最新的评价

       $new_num = $this->obj->DB_select_once('comments',"com_id='$com_id' and type=1 and addtime>UNIX_TIMESTAMP(CAST(SYSDATE()AS DATE))",'count(*) as new_num');

       $new_num = $new_num['new_num'];

       $this->yunset('good_num',$good_num);
       $this->yunset('bad_num',$bad_num);
       $this->yunset('new_num',$new_num);




       $comments = $this->obj->DB_select_all('comments',"`com_id`='".(int)$com_id."' and type=1 order by id desc limit 3");
        
       // print_R($comments);exit;
        $comments_item= array();
        $commentes= array();
        foreach($comments as $k=>$v){

		     $userinfo=$UserinfoM->GetMemberOne(array("uid"=>(int)$v['uid']),array("field"=>"`username`,`head_img`"));
             $comments_item['username']= $this->substr_cut($userinfo['username']);
             $comments_item['userhead'] = $userinfo['head_img'];

             $jobinfo = $this->obj->DB_select_once(
			  "company_job",
			  "`id`='{$v['jobid']}'",
			  "`name`"

			);

             $comments_item['job_name'] = $jobinfo['name'];
             $score = array();
             for($i=1;$i<=5;$i++){

             	if($i<=$v['zh_lv']){
                  $score[] = 1;
             	}else{
             	  $score[] = 0; 
             	}

             }
             $comments_item['score'] = $score;
             $label_arr=array();

             foreach($comclass_name as $key=>$val){
             	$tem_arr = explode(',',$v['labels']);

                    if(in_array($key ,$tem_arr)){

                    	$label_arr[]= $val;
                    }
                
             }

             $comments_item['label'] = $label_arr;

             $comments_item['date'] = date('m月-d日',$v['addtime']);
             $comments_item['cont'] = $v['comment_desc'];
             $commentes[] =  $comments_item;
		      
        }

       //print_R($commentes);exit;
         
       $this->yunset('commentes', $commentes);

       $this->yunset("headertitle","全部评价");
	   $this->yunset("shareurl",Url('wap',array('c'=>'company','a'=>'share','id'=>$row['uid'])));
	   $this->yuntpl(array('wap/company_pj_list'));
	   
      
   }


   function ajax_list_action(){
      $com_id= $_POST['comid'];
      $page = $_POST['page'];
      $type = $_POST['type'];  //1最新  2好评 3差评
      $pagesize=5;

      $offset = ($page-1)*$pagesize;
     
      //echo $offset;exit;

      $UserinfoM=$this->MODEL('userinfo');
      $resumeM=$this->MODEL('resume');
      $CacheM=$this->MODEL('cache');

      $CompanyM=$this->MODEL('company');

      $CacheList=$CacheM->GetCache(array('job','com','city','hy'));
      include PLUS_PATH."com.cache.php";
      $where = "";

      if($type==2){ //好评
      	 $where.=' and c.zh_lv > 3';
      }

      if($type==3){ //差评
         $where.=' and c.zh_lv <= 3';
      }


       //$comments = $this->obj->DB_select_all('comments',"`com_id`='".(int)$com_id."' and type=1 {$where} order by id desc limit ". $offset. ", $pagesize");

         $sql="select c.*,count(cl.comment_id) as zan_num from {$this->def}comments c left join {$this->def}comments_like cl  on c.id=cl.comment_id and cl.status=1 where c.com_id='$com_id' and type=1  {$where} group by c.id order by c.id desc limit $offset ,$pagesize";

         //echo $sql;exit;
         $comments = $this->db->query($sql);
          
        // print_R($this->db->fetch_array($comments));exit;


        $comments_item= array();
        $commentes= array();

        //foreach($comments as $k=>$v){
         while($v = $this->db->fetch_array($comments)) {

		     $userinfo=$resumeM->SelectResume(array("uid"=>(int)$v['uid']),'`name`,`resume_photo`,`sex`');

              //print_R($userinfo);exit;
             $comments_item['id'] = $v['id'];
             $comments_item['username']= $this->substr_cut($userinfo['name']);
             $comments_item['userhead'] = strpos($userinfo['resume_photo'],'http')===false?$this->config['sy_weburl'].$userinfo['resume_photo']:$userinfo['resume_photo'];

             $comments_item['sex'] = $userinfo['sex'];

             $jobinfo = $this->obj->DB_select_once(
			  "company_job",
			  "`id`='{$v['jobid']}'",
			  "`name`"

			);

             $comments_item['job_name'] = $jobinfo['name'];
             $score = array();
             for($i=1;$i<=5;$i++){

             	if($i<=$v['zh_lv']){
                  $score[] = 1;
             	}else{
             	  $score[] = 0; 
             	}

             }
             $comments_item['score'] = $score;
             $label_arr=array();
             foreach($comclass_name as $key=>$val){
             	$tem_arr = explode(',',$v['labels']);

                    if(in_array($key ,$tem_arr)){

                    	$label_arr[]= $val;
                    }
                
             }

             $comments_item['label'] = $label_arr;

             $comments_item['date'] = date('m月-d日',$v['addtime']);
             $comments_item['cont'] = $v['comment_desc'];
             $comments_item['zan_num'] = $v['zan_num'];
             $zan_info =  $this->obj->DB_select_once('comments_like',"uid='{$this->uid}' and comment_id='{$v['id']}'");

            $comments_item['is_like'] = $zan_info && $zan_info['status']==1?1:0;



             $commentes[] =  $comments_item;
		      
        }

        //print_R($commentes);exit;

        echo json_encode($commentes);die;

   }




	function show_action(){

        //die('show_action');
        
		$this->rightinfo();
		$this->get_moblie();
        $UserinfoM=$this->MODEL('userinfo');
        $CacheM=$this->MODEL('cache');
        $resumeM=$this->MODEL('resume');
        $CompanyM=$this->MODEL('company');

        $CacheList=$CacheM->GetCache(array('job','com','city','hy'));
        include PLUS_PATH."com.cache.php";
        //hjl 获取企业职位标签 start
         //print_r($CacheList['city_name']);exit;

         //print_R($CacheList['comclass_name']);exit;
         //print_R($CacheList['comdata']['job_label']);exit;
         $all_label = array();
         foreach($CacheList['comclass_name'] as $k=>$v){
             if(in_array($k,$CacheList['comdata']['job_label'])){
             	 $all_label[$k] = $v;
             }
         }

         //print_r($job_label);exit;
          $com_label=$this->obj->DB_select_all("company_job","`uid`='{$_GET['id']}'",'label');

          //print_R( $com_label);exit;
          $job_label= array();
          foreach($com_label as $k=>$v){
               $cur_label = explode(',', $v['label']);
               $job_label  = empty($job_label)?$cur_label: array_merge($job_label, $cur_label);

          }

          $job_all_label=array();
          foreach($all_label as $k=>$v){
             if(in_array($k,$job_label)){
               $job_all_label[$k] = $v;
             }

          }

          //print_R($job_all_label);exit;

          $this->yunset('labels',$job_all_label);
          
        //hjl 获取企业职位标签 end


         //获取收藏过的职位

        $fav_jobs=$this->obj->DB_select_all("fav_job","`uid`='{$this->uid}'",'uid,job_id');
        
        
        $fav_arr = array();

        foreach($fav_jobs as $v){

           $fav_jobids[] = $v['job_id'];
        }

        print_R($fav_jobids);exit;

        $this->yunset('fav_jobids',$fav_jobids);


		$this->yunset($CacheList);

        //hjl 获取企业产品 start
        $ProductList=$CompanyM->GetProductAll(array('status'=>1,'uid'=>$_GET['id']));

        //print_R($ProductList);exit;

        foreach($ProductList as $k=>$v){
        	$ProductList[$k]['welfare']= explode(',', $v['welfare']); 
        }

        $this->yunset('ProductList',$ProductList);

        //hjl 获取企业产品 end
       
        //hjl 获取管理团队
        $teams =$this->obj->DB_select_all('company_team',"`uid`='".(int)$_GET['id']."'");
        
        $this->yunset('teams',$teams);

        //hjl 获取工商信息
         
          $gs_info =$this->obj->DB_select_once('company',"`uid`='".(int)$_GET['id']."'",'name,money,moneytype,sdate,legal_person');

          //print_R($gs_info);exit;
          $this->yunset('gs_info',$gs_info);


         //hjl 获取公司地址
         
         $com_adds = $this->obj->DB_select_all('addcompanyaddress',"`uid`='".(int)$_GET['id']."'");
         $com_adds_list = array();

         foreach($com_adds as $k=>$v){
         	 $com_address_list[$k]['address']= $CacheList['city_name'][$v['cityid']].'-'.$CacheList['city_name'][$v['three_cityid']];
         	 $com_address_list[$k]['add_detail'] = $v['address'];
         }

        $this->yunset('address_list',$com_address_list);

        //hjl 获取评价列表
        
        //$comments = $this->obj->DB_select_all('comments',"`com_id`='".(int)$_GET['id']."' and type=1 order by id desc limit 3");
        
        $sql="select c.*,count(cl.comment_id) as zan_num from {$this->def}comments c left join {$this->def}comments_like cl  on c.id=cl.comment_id and cl.status=1 where c.com_id='{$_GET['id']}' and type=1  group by c.id order by c.zh_lv desc,c.id desc limit 3";
        $query = $this->db->query($sql);
        
        
        //print_R($comments);exit;
        $comments_item= array();
        $commentes= array();
        //foreach($comments as $k=>$v){
        while($v = $this->db->fetch_array($query)) {
		     $userinfo=$resumeM->SelectResume(array("uid"=>(int)$v['uid']),'`name`,`resume_photo`,`sex`');

              //print_R($userinfo);exit;
             $comments_item['id'] = $v['id'];
             $comments_item['username']= $this->substr_cut($userinfo['name']);
             $comments_item['userhead'] = strpos($userinfo['resume_photo'],'http')===false?$this->config['sy_weburl'].$userinfo['resume_photo']:$userinfo['resume_photo'];

             $comments_item['sex'] = $userinfo['sex'];

             $jobinfo = $this->obj->DB_select_once(
			  "company_job",
			  "`id`='{$v['jobid']}'",
			  "`name`"

			);


              $label_arr=array();
             foreach($comclass_name as $key=>$val){
              $tem_arr = explode(',',$v['labels']);

                    if(in_array($key ,$tem_arr)){

                      $label_arr[]= $val;
                    }
                
             }

            $comments_item['label'] = $label_arr;

             $comments_item['job_name'] = $jobinfo['name'];
             $score = array();
             for($i=1;$i<=5;$i++){

             	if($i<=$v['zh_lv']){
                  $score[] = 1;
             	}else{
             	  $score[] = 0; 
             	}

             }
             $comments_item['score'] = $score;

             $comments_item['date'] = date('m月-d日',$v['addtime']);
             $comments_item['cont'] = $v['comment_desc'];
             $comments_item['zan_num'] = $v['zan_num'];
             $zan_info =  $this->obj->DB_select_once('comments_like',"uid='{$this->uid}' and comment_id='{$v['id']}'");

             $comments_item['is_like'] = $zan_info && $zan_info['status']==1?1:0;

             $commentes[] =  $comments_item;
		      
        }
         
         $this->yunset('commentes', $commentes);

         //print_R($commentes);exit;





		$ComMember=$UserinfoM->GetMemberOne(array("uid"=>(int)$_GET['id']),array("field"=>"`status`"));
		$row=$UserinfoM->GetUserinfoOne(array('uid'=>(int)$_GET['id']),array('usertype'=>2));
		$show =$this->obj->DB_select_all('company_show',"`uid`='".(int)$_GET['id']."'");	
		if($show&&is_array($show)){
			foreach($show as $k=>$v){
				$show[$k]['picurl']=str_replace('./','/',$v['picurl']);
			}
		}
        $this->yunset('show', $show);	
		
		if(!is_array($row)){
			$this->ACT_msg($_SERVER['HTTP_REFERER'],"没有找到该企业！");
		}elseif($ComMember[status]==0){
			$this->ACT_msg($_SERVER['HTTP_REFERER'],"该企业正在审核中，请稍后查看！");
		}elseif($ComMember[status]==3){
			$this->ACT_msg($_SERVER['HTTP_REFERER'],"该企业未通过审核！");
		}elseif($row[r_status]==2){
			$this->ACT_msg($_SERVER['HTTP_REFERER'],"该企业暂被锁定，请稍后查看！");
		}
		$linkphone = explode('-', $row['linkphone']);
		if(strlen($linkphone[0])==4){
		    $row['callphone']=$linkphone[0].$linkphone[1];
		}else if(strlen($linkphone[0]>8)){
		    $row['callphone']=substr($row['linkphone'], 0, 12);
		}else{
		    $row['callphone']=$row['linkphone'];
		}
		
		if($row['infostatus']==2){
			$look_msg="企业没有公开联系信息！";
			$looktype="3";
		}elseif($this->config['com_login_link']=='2'){
			$look_msg="网站没有开放企业联系信息！";
			$looktype="2";
		}elseif($this->config['com_login_link']=='1' || $this->uid==$_GET['id']){
			$looktype="1";
		}elseif($this->uid=='' && $this->username==''){
			if($row['linktel']){
				$row['linktel']= substr_replace($row['linktel'],'****',4,4);
			}
			if($row['linkphone']){
				$row['linkphone']= substr_replace($row['linkphone'],'****',4,4);
			}
			$looktype="4";
		}elseif($this->usertype!=1&&(int)$_GET['id']!=$this->uid){
			$look_msg="您不是个人用户，不能查看联系方式";
			$looktype="5";
		}elseif($this->config['com_login_link']=="3" && $this->uid){
 			$looktype="1";
		}elseif($this->config['com_login_link']=="4" && $this->usertype==1){
			$Resume=$this->MODEL('resume');
			$resumenum=$Resume->GetResumeExpectNum(array("uid"=>$this->uid));
			if($resumenum<1){
				$look_msg="您还没有创建简历，不能查看联系方式！";
				$looktype="6";
			}else{
				$looktype="1";
			}
		}elseif($this->config['com_login_link']=="5" && $this->usertype==1){
	         $sendresume=$this->obj->DB_select_num("userid_job","`uid`='".$this->uid."' and `com_id`='".(int)$_GET['id']."'");
	        if($sendresume<1){
	           	$look_msg="投递简历，联系电话任意看"; 
				$looktype="7";
	        }else{
	        	$looktype="1";
	        }
	   	}else{
			$looktype="1";
		}
		
		$this->yunset("look_msg",$look_msg);
		$this->yunset("looktype",$looktype);
		
		$rows=$UserinfoM->GetUserstatisOne(array('uid'=>(int)$row['uid']),array("field"=>"`rating`","usertype"=>2));		
		$comrat=$UserinfoM->GetRatinginfoOne(array("id"=>$rows['rating']));
		if($comrat['com_pic']&&file_exists(APP_PATH.$comrat['com_pic'])){
			$comrat['com_pic']=$this->config['sy_weburl']."/".$comrat['com_pic'];
		}else{
			$comrat['com_pic']='';
		}
		
			
		
		$row['lastupdate']=date("Y-m-d",$row['lastupdate']);
		if($row['infostatus']=='2'){
			$row['linkphone']=$row['linktel']=$row['linkmail']=$row['linkqq']='';
		}
		if(!$row['logo'] || $row['logo_status']!='0' ||!file_exists(str_replace($this->config['sy_weburl'],APP_PATH,'.'.$row['logo']))){
		    $row['logo']=$this->config['sy_weburl']."/".$this->config['sy_unit_icon'];
		}else{
		    $row['logo']=str_replace("./",$this->config['sy_weburl']."/",$row['logo']);
		}
		$welfare = @explode(",",$row['welfare']);
		foreach($welfare as $k=>$v){
			if(!$v){
				unset($welfare[$k]);
			}
		}
		$row['welfare_n']=$welfare;
		
		$row['content']=str_replace(array("ti<x>tle","“","”"),array("title"," "," "),$row['content']);
		$row['content']=htmlspecialchars_decode($row['content']);
		preg_match_all('/<img(.*?)src=("|\'|\s)?(.*?)(?="|\'|\s)/',$row['content'],$res);
		if(!empty($res[3])){
		    foreach($res[3] as $v){
		        if(strpos($v,'http:')===false && strpos($v,'https:')===false){
		            
		            $row['content'] = str_replace($v,$this->config['sy_weburl'].$v,$row['content']);
		        }
		    }
		}
		$this->yunset("row",$row);
		$this->yunset("comrat",$comrat);
		if($this->uid&&$this->usertype=='1'){
		    $AskM=$this->MODEL('ask');
		    $isatn=$AskM->GetAtnOne(array("uid"=>$this->uid,"sc_uid"=>(int)$_GET['id']));
		    $this->yunset("isatn",$isatn);
		}
		if ($this->usertype==1){
		    $jobM = $this->MODEL('job');
		    $userid_job = $jobM->GetUseridJobOne(array('uid'=>$this->uid,'com_id'=>(int)$_GET['id']));
		    $this->yunset('userid_job', $userid_job);
		}
		$data['company_name']=$row['name'];
		$data['company_name_desc']=$row['content'];
		$this->data=$data;
		$this->seo("company_index");
		$this->yunset("headertitle","公司详情");
		$this->yunset("shareurl",Url('wap',array('c'=>'company','a'=>'share','id'=>$row['uid'])));
		$this->yuntpl(array('wap/new_company_show'));
	}


    //hjl add 
    function substr_cut($user_name){  


        //获取字符串长度  
        $strlen = mb_strlen($user_name, 'utf-8');  
        //如果字符创长度小于2，不做任何处理  
        if($strlen<2){  
            return $user_name;  
        }else{  
            //mb_substr — 获取字符串的部分  
            $firstStr = mb_substr($user_name, 0, 1, 'utf-8');  
            $lastStr = mb_substr($user_name, -1, 1, 'utf-8');  
            //str_repeat — 重复一个字符串  
            return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;  
        }  
} 
    








	function share_action(){
		$this->get_moblie();
		$M=$this->model("company");
		$row=$M->GetCompanyInfo(array('uid'=>(int)$_GET['id']));
		$welfare = @explode(",",$row['welfare']);
		foreach($welfare as $k=>$v){
			if(!$v){
				unset($welfare[$k]);
			}
		}
		$row['welfare']=$welfare;
		$row['content']=strip_tags($row['content']);
		if($row['logo']){
			$row['logo']=str_replace("./",$this->config['sy_weburl']."/",$row['logo']);
		}  
		if($this->uid=="" && $this->username==""){
		    $look_msg=1;
		}elseif($this->uid!=$row['uid']){
			if($this->config['com_login_link']=="2"){
			    $look_msg=4;
			}elseif($this->config['com_login_link']=="3"){
				if($this->usertype!="1"){
					$look_msg=2;
				}
				if($this->config['com_resume_link']=="1"&&$this->usertype=='1'){
					$ResumeM=$this->MODEL('resume');
					$num=$ResumeM->GetResumeExpectNum(array("uid"=>$this->uid)); 
					if($num<1){
						$look_msg=3;
					}
				}
			}
		}
		$this->yunset("look_msg",$look_msg);
		$this->yunset("row",$row);
		$show=$M->GetShowAll(array('uid'=>(int)$_GET['id']),array("field"=>"`picurl`"));
		if(is_array($show)){
			foreach($show as $k=>$v){
				$show[$k]['picurl']=str_replace("./",$this->config['sy_weburl']."/",$v['picurl']);
			}
		}
		$this->yunset("show",$show);
		$product=$M->GetProductAll(array('uid'=>(int)$_GET['id'],"status"=>"1"));
		$this->yunset("product",$product);
		$CacheM=$this->MODEL('cache');
        $CacheList=$CacheM->GetCache(array('job','com','city','hy'));

		$this->yunset($CacheList);
		$data['company_name']=$row['name'];
		$data['company_name_desc']=$row['content'];
		$this->data=$data;
		$this->seo("company_index");
		$this->yunset("company_style",$this->config['sy_weburl']."/app/template/wap/company");
		$this->yuntpl(array('wap/company/index'));
	}

	function msg_action(){
		$this->rightinfo();
		$this->get_moblie();
		$UserinfoM=$this->MODEL('userinfo');
		$row=$UserinfoM->GetUserinfoOne(array('uid'=>(int)$_GET['id']),array('usertype'=>2));
		$this->yunset("row",$row);
		$data['company_name']=$row['name'];
		$data['company_name_desc']=$row['content'];
		$this->data=$data;
		$this->seo("company_index");
		$this->yunset("headertitle","公司详情");
		$this->yuntpl(array('wap/company_msg'));
	}	
 
}
?>